// JavaScript Document
$(document).ready(function(){
						   $('#btnAddGroupRole').click(function(){
														   $('#updateGroup h3').html('Tạo mới nhóm');
														  
														  ResetModalUser();
														  $('#task').val('add');
														  
														   });
						    $('#tbResult a.update').live('click',function(){
																		  $('#updateGroup h3').html('Cập nhật nhóm');
																		  
															ResetModalUser();
															$('#task').val('update');
															$('#groupid').val($(this).parent().parent().children().eq(0).text());
															$('#txtGroupRoleName').val($(this).parent().parent().children().eq(1).text());
															$('#txtDescription').val($(this).parent().parent().children().eq(2).text());
															
																		  });
							 $('#tbResult a.delete').live('click',function(){
											
											if(confirm('Bạn thực sự muốn xóa?'))
											{
													var id=$(this).parent().parent().children().eq(0).text();
													var row=$(this).parent().parent();
													$.ajax({
														 url:"../Admin/process/GroupRole_process.php",
														 data:{groupid: id,task:"delete"},
														 type:"POST",
														 dataType:"json",
														 cache:false,
														 success:function(res){
															if(res.ok)
															{
																row.remove();
															}
															alert(res.msg);
															 }
														 });
											}																	   
																		   });
							  $('#tbResult tr.active').live('click',function(){
												$('#tbUsers tr').remove();		
												$('#tbRoles tr').remove();	
												$('#btnAddUser').removeClass('hide');
												$('#btnAddRole').removeClass('hide');
												var name=$(this).children().eq(1).text();
												var id=$(this).children().eq(0).text();
													$('#GroupName').text('Chi tiết '+name);	
													$('#grouproleID').val(id);
													//get list of users
													$.ajax({
														 url:"../Admin/process/GroupRole_process.php",
														 data:{groupid: id,task:"getUser"},
														 type:"POST",
														 dataType:"json",
														 cache:false,
														 success:function(res){
															if(res.length>0)
															{
													for(var i=0;i<res.length;i++)	
												{
$('#tbUsers').append('<tr><td>'+res[i].username+'</td><td>'+res[i].fullname+'</td><td><a href="#" class="deleteUser" title="Xóa"><img src="../Images/Icons/delete.png" /></a></td></tr>');
												}
															}
															
															 }
														 });
												//get list of roles
													$.ajax({
														 url:"../Admin/process/GroupRole_process.php",
														 data:{groupid: id,task:"getRole"},
														 type:"POST",
														 dataType:"json",
														 cache:false,
														 success:function(res){
															if(res.length>0)
															{
													for(var i=0;i<res.length;i++)	
												{
$('#tbRoles').append('<tr><td class="hide">'+res[i].roleid+'</td><td>'+res[i].rolename+'</td><td><a href="#" class="deleteRole" title="Xóa"><img src="../Images/Icons/delete.png" /></a></td></tr>');
												}
															}
															
															 }
														 });
																			});
							  
							  
				$('#btnAddUser').click(function(){
							$('#tbSelectUsers tr:not(:first)').remove();
							$("#selectAllUsers").removeAttr('checked');
												});
				$('#btnAddRole').click(function(){
							$('#tbSelectRoles tr:not(:first)').remove();
							$("#selectAllRoles").removeAttr('checked');
												});
			//-------------------------------Search User----------------------------------------				  
			$('#btnSearchUser').click(function(){
								var id=$('#grouproleID').val();
								var keyword=$('#txtUserKey').val();
								
					$.ajax({
							url:"../Admin/process/Account_add.php",
							data:{groupid: id,keyword:keyword,task:"searchgroup"},
							type:"POST",
							dataType:"json",
							cache:false,
							success:function(res){
							$('#tbSelectUsers tr:not(:first)').remove();
							if(res.length>0)
							{
								for(var i=0;i<res.length;i++)	
												{
$('#tbSelectUsers').append('<tr><td><input type="checkbox"/></td><td>'+res[i].username+'</td><td>'+res[i].fullname+'</td></tr>');
												}
							}
															
								}
										});
									});
	//-------------------------------Search Role----------------------------------------				  
			$('#btnSearchRole').click(function(){
								var id=$('#grouproleID').val();
								var keyword=$('#txtRoleKey').val();
								
					$.ajax({
							url:"../Admin/process/Role_process.php",
							data:{groupid: id,keyword:keyword,task:"searchgroup"},
							type:"POST",
							dataType:"json",
							cache:false,
							success:function(res){
							$('#tbSelectRoles tr:not(:first)').remove();
							if(res.length>0)
							{
								for(var i=0;i<res.length;i++)	
												{
$('#tbSelectRoles').append('<tr><td><input type="checkbox"/></td><td>'+res[i].roleid+'</td><td>'+res[i].rolename+'</td></tr>');
												}
							}
															
								}
										});
									});
<!-----------------------------------------Checkbox select all----------------------------------------------->
						 $("#selectAllRoles").click(function() {
									var checkedStatus = this.checked;
						$("#tbSelectRoles tbody tr td:first-child input:checkbox").each(function() {
										this.checked = checkedStatus;
									});
								});
						 
						 $("#selectAllUsers").click(function() {
									var checkedStatus = this.checked;
						$("#tbSelectUsers tbody tr td:first-child input:checkbox").each(function() {
										this.checked = checkedStatus;
									});
								});
<!-----------------------------------------Insert selected users into Group------------------------------------------>
					$('#addUsers').click(function(){
							var list='';
							$("#tbSelectUsers tr:not(:first) td:first-child input:checkbox").each(function() {
										if(this.checked)
										{
											var user=$(this).parent().parent().children().eq(1).text();
											list+=user+';';
										}
									});
							if(list!='')
							{
								var id=$('#grouproleID').val();
								$.ajax({
									    url:"../Admin/process/GroupRole_process.php",
										data:{groupid: id,task:"insertuser",users:list},
										type:"POST",
										cache:false,
										success:function(res){
											alert(res);
											$('#updateUsers').modal('hide')
											}
									   });
							}
							//alert(list);
												  });
<!-----------------------------------------Insert selected roles into Group------------------------------------------>
					$('#addRoles').click(function(){
							var list='';
							$("#tbSelectRoles tr:not(:first) td:first-child input:checkbox").each(function() {
										if(this.checked)
										{
											var role=$(this).parent().parent().children().eq(1).text();
											list+=role+';';
										}
									});
							if(list!='')
							{
								var id=$('#grouproleID').val();
								$.ajax({
									    url:"../Admin/process/GroupRole_process.php",
										data:{groupid: id,task:"insertrole",roles:list},
										type:"POST",
										cache:false,
										success:function(res){
											alert(res);
											$('#updateRoles').modal('hide')
											}
									   });
							}
							//alert(list);
												  });
<!-----------------------------------------Delete User------------------------------------------>
					$('#tbUsers a.deleteUser').live('click',function(){
							if(confirm('Bạn thực sự muốn xóa?'))
											{
								var user=$(this).parent().parent().children().eq(0).text();
								var id=$('#grouproleID').val();
													var row=$(this).parent().parent();
													$.ajax({
														 url:"../Admin/process/GroupRole_process.php",
														 data:{groupid: id,task:"deleteUser",user: user},
														 type:"POST",
														 dataType:"json",
														 cache:false,
														 success:function(res){
															if(res.ok)
															{
																row.remove();
															}
															alert(res.msg);
															 }
														 });
											}
												  });
	<!-----------------------------------------Delete Role------------------------------------------>
					$('#tbRoles a.deleteRole').live('click',function(){
							if(confirm('Bạn thực sự muốn xóa?'))
											{
								var role=$(this).parent().parent().children().eq(0).text();
								var id=$('#grouproleID').val();
													var row=$(this).parent().parent();
													$.ajax({
														 url:"../Admin/process/GroupRole_process.php",
														 data:{groupid: id,task:"deleteRole",role: role},
														 type:"POST",
														 dataType:"json",
														 cache:false,
														 success:function(res){
															if(res.ok)
															{
																row.remove();
															}
															alert(res.msg);
															 }
														 });
											}
												  });
						   });
function ResetModalUser(){
	$('#txtGroupRoleName').val('');
	$('#txtDescription').val('');
	
	}